// pages/detailed/detailed.ts
const detailedApp = getApp();

Page({
    /**
     * 页面的初始数据
     */
    data: {
        voteId: '',
        check: false,
        participate: false,
        isVote: false,
        voteInfo: {},
        options: [{}],
        value: []
    },

    getValue(e: any) {
        console.log(e)
        this.setData({
            value: {
                voId: e.detail.value,
                vsId: this.data.voteInfo.voteSubject.vsId,
                vuUserId: detailedApp.globalData.voteUser.vuUserId,
                type: e.currentTarget.id
            }
        });
        console.log(this.data.value);
    },

    doVote() {
        console.log(JSON.stringify(this.data.value))
        wx.request({
            url: detailedApp.globalData.backgroundURL + 'item/doInsert',
            data: {
                jsonStr: JSON.stringify(this.data.value)
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'POST',
            success: (res) => {
                if(res.data){
                    this.doAttend();
                    this.getVoteInfo();
                }
            }
        });
    },

    radioChange: function (e: any) {
        const selectedValue = e.detail.value;
        const options = this.data.options.map(item => {
            if (item.value === selectedValue) {
                item.checked = true;
            } else {
                item.checked = false;
            }
            return item;
        });
        this.setData({
            options: options
        });
    },

    doAttend() {
        console.log(detailedApp.globalData.isLogin)
        if (!detailedApp.globalData.isLogin) {
            wx.navigateTo({
                url: ('../login/login')
            });
            return;
        }
        this.setData({
            participate: !this.data.participate
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            voteId: options.id
        });
        this.getVoteInfo();
    },

    getVoteInfo() {
        wx.request({
            url: detailedApp.globalData.backgroundURL + 'vote-option/view',
            data: {
                id: this.data.voteId
            },
            method: 'GET',
            success: (res) => {
                console.log(res.data);
                let jsons = [{}];
                res.data.voteItemService.forEach(element => {
                    jsons.push({
                        name: element.voOption,
                        value: element.voId,
                        checked: false
                    });
                    element.voteItems.forEach(item => {
                        if(item.vuUserId === detailedApp.globalData.voteUser.vuUserId){
                            console.log(item);
                            this.setData({
                                isVote: !this.data.isVote
                            });
                        }
                    });
                });
                this.setData({
                    voteInfo: res.data,
                    options: jsons
                });
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})